<template>
	<div class="evaluate-add">
		<div class="classIndex_h">
		    <router-link  class="fade-router" :to="{path: '/class/quality/index', query: {id: path}}">综合素质评价</router-link>
		    <router-link  class="fade-router" :to="{path: '/class/quality/manage', query: {id: path}}"><i>></i>评价管理</router-link>
		    <strong><i>></i>编辑</strong>
		</div>

		<div class="quality-seleter">
			<span>学期：</span>
			<el-select v-model="semester" clearable  placeholder="请选择" @change="changeQuality">
			   <el-option  
			     v-for="(item, index) in SemesterList"
			     :key="index"
			     :label="item.name"
			     :value="item.id">
			   </el-option>
			 </el-select>
		</div>

		<div class="quality-statistics ">
			<div class="quality-title evaluate-title"><i class="iq q-6"></i><span>教师评委</span></div>
			<div class="quality-user">
				<span v-for="(item, index) of teacherList" :key="index">
					
					<i class="iq q-8" v-if="$store.getters.userInfo.is_headmaster == 1 & $g.user_id == item.id"></i>
					<i class="el-icon-close" v-else @click="closeUser(6, index)"></i>
					{{item.name}}
				</span>
				<button @click="changUser(1)">
					<i class="el-icon-plus"></i>
				</button>
			</div>
			
		</div>

		<div class="quality-statistics">
			<div class="quality-title"><i class="iq q-7"></i><span>学生评委</span></div>
			<div class="quality-user">
				<span v-for="(item, index) of studentList" :key="index">
					<i class="el-icon-close" @click="closeUser(4, index)"></i>
					{{item.name}}
				</span>
				<button @click="changUser(2)">
					<i class="el-icon-plus"></i>
				</button>
			</div>
			
		</div>

		<div class="evaluate-date">
			<span>截止时间：</span>
			<el-date-picker
			      v-model="date"
			      type="date"
			       value-format="yyyy-MM-dd"
			      :picker-options='pickerOptions'
			      placeholder="结束日期">
			    </el-date-picker>
		</div>

		<div  class="evaluate-btn">
			<el-button type="primary" @click="createEvaluation">创建</el-button>
		</div>

		<el-dialog
			:title="judgesName"
			:visible.sync="selectTestView"
			width="800px"
			custom-class="alBox"
			:close-on-click-modal="false">
			<div class="releaseTK">
				<div class="releaseTK_h clear">
					<!--  -->
					<div class="resourceSearch fl">
						<el-input placeholder="用户名" v-model="keyword" @change="changeSearch">
						  <el-button slot="append" icon="el-icon-search"></el-button>
						</el-input>
					</div>
				</div>
				<!--  -->
				<div class="resourceData">
					<template>
						<el-table
						border
						ref="testChecked"
						:data="userNameList"
						tooltip-effect="dark"
						style="width: 100%"
						@selection-change="changeTestChecked">
							<el-table-column
							type="selection"
							width="45">
							</el-table-column>
							<el-table-column
							prop="number"
							label="登录账号">
							</el-table-column>
							<el-table-column
							prop="name"
							label="用户名"
							width="250">
							</el-table-column>
							<el-table-column
							prop="create_time"
							label="性别"
							width="150"
							show-overflow-tooltip>
							<template slot-scope="scope">
								<span v-if="scope.row.sex == 1">男</span>
								<span  v-if="scope.row.sex == 2">女</span>
							</template>
							</el-table-column>
						</el-table>
					</template>
					<div class="pageTable" v-if=" sunTotal > 10">
						<el-pagination
						    @current-change="changePage"
						    :page-size="size"
						    :current-page="page"
						    layout="prev, pager, next"
						    :total="sunTotal">
						  </el-pagination>
					</div>
				</div>
			</div>
			<div slot="footer" class="dialog-footer buttonCSS">
				<el-button type="primary" @click="closevisible" plain>取 消</el-button>
				<el-button type="primary" @click="submitUser">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	
	export default {
		data(){
			return {
				pickerOptions: {
					disabledDate:(time) => {
						return time.getTime() < Date.now() - 8.64e7;
					}
				},
				date: '',
				semester: '',
				judgesName: '选择评委',
				testChecked: [],
				SemesterList: [],
				teacherList: [],
				studentList: [],
				userNameS: [],
				userNameT: [],

				evaluateList: {
					teacher: [],
					student: []
				},

				selectTestView: false,
				userList: [],
				keyword: '',
				size: 10,
				page: 1,
				sunTotal: 0,
				path: sessionStorage.getItem('classId'),
				role: '',
			}
		},
		watch: {
			'$store.state.changData': {
				handler(val, newval){
					this.path = sessionStorage.getItem('classId')
					this.getSemester()
					this.getUserList(4)
					this.getUserList(6)
					this.studentList = []
					this.teacherList = []
					this.userNameList = []
				},
				immediate: true
			}
		},
		created(){
			
			this.getCreated()
		},
		methods: {
			getEvaluate(){
				let success = ( res => {

					this.date = res.data[0].end_time
					this.semester = res.data[0].term_id
					this.evaluateList.teacher = res.data[0].teacher.split(',')
					this.evaluateList.student = res.data[0].student.split(',')
					this.teacherList =  []
					this.studentList = []
					for (let i = 0; i < this.userNameT.length; i++) {
						if (this.evaluateList.teacher.includes(this.userNameT[i].id+'')) {
							this.teacherList.push({
								id: this.userNameT[i].id,
								name: this.userNameT[i].name
							})
						}
					}

					for (let i = 0; i < this.userNameS.length; i++) {
						if ( this.evaluateList.student.includes(this.userNameS[i].id+'') ) {
							this.studentList.push({
								id: this.userNameS[i].id,
								name: this.userNameS[i].name
							})
						}
					}

				});
				this.$ajax({
					url: 'api/quality/quality',
					method: 'get',
					params: {
						term_id: this.$route.query.tid,
						class_id: this.$route.query.id,
					},
					func: {
						success: success
					}
				})
			},
			/*
				评价管理--编辑
			*/
			getCreated(){
				let success  =(res=>{

				});

				let params = {
					term_id: this.$route.query.tid,
					class_id: this.$route.query.id,
					// user_id: ,
				};

				this.$ajax({
					url: 'api/quality/quality',
					method: 'get',
					params: params,
					func: {success: success}
				})
			},
			/*新建评价指标*/
			createEvaluation(){

				if (this.semester == '') {
					return this.$message.error('请选择学期')
				}else if (this.teacherList.length == 0 ) {
					return this.$message.error('请选择教师评委')
				}else if (this.studentList.length == 0 ) {
					return this.$message.error('请选择学生评委')
				}else if (this.date == '') {
					return this.$message.error('请选择截止时间')
				}

				let [teacherid, studentid] = [[], []]

				for (let i = 0; i < this.teacherList.length; i++) {
					teacherid.push(this.teacherList[i].id)
				}

				for (let j = 0; j < this.studentList.length; j++) {
					studentid.push(this.studentList[j].id)
				}

				let success = (res=>{
					this.$message.success(res.msg);
					this.$router.back(-1)
				});

				let params = {
					term_id: this.semester,
					class_id: this.path,
					end_time: this.date,
					teacher_id: teacherid,
					student_id: studentid
				};

				this.$ajax.put('api/quality/quality', params, {func: {success: success}})
			},
			/*删除已选择的评委*/
			closeUser(type, index){
				if (type == 6) {
					this.teacherList.splice(index,1)
				}else{
					this.studentList.splice(index,1)
				}
			},
			/*关闭选择用户窗口*/
			closevisible(){
				this.testChecked = []
				this.selectTestView = false
			},
			/*提交选择的评委据*/
			submitUser(){
				// this.teacherList = []
				// this.studentList = []
				if (this.testChecked.length == 0) {
					return this.$message.error('请勾选')
				}

				if (this.role == 6) {

					for (let i = 0; i < this.testChecked.length; i++) {
						this.teacherList.push({
							id: this.testChecked[i].id,
							name: this.testChecked[i].name
						})
					};

					/*去重 */
					let hash = {};
					let arr =  this.teacherList.reduceRight((item, next)=>{
					    hash[next.id] ? '' : hash[next.id] = true && item.push(next);
					    return item
					}, [])

					this.teacherList = arr;
					this.selectTestView = false

				}else{
					for (let i = 0; i < this.testChecked.length; i++) {
						this.studentList.push({
							id: this.testChecked[i].id,
							name: this.testChecked[i].name
						})
					};

					/*去重 */
					let hash = {};
					let arr =  this.studentList.reduceRight((item, next)=>{
					    hash[next.id] ? '' : hash[next.id] = true && item.push(next);
					    return item
					}, [])

					this.studentList = arr;
					this.selectTestView = false
				}

			},
			//老师
			getUserList(type){
				let success = (res=>{
					let data = res.data;
					this.userNameList = data.data;
					this.sunTotal = res.data.data_sum;
					this.page = parseInt(res.data.page_index);
					if (type == 6) { 
						this.judgesName= '选择教师评委'
						this.userNameT = data.data;
					}else{
						this.judgesName= '选择学生评委'
						this.userNameS = data.data;
					}
					
				});

				let params = {
					classid: this.path,
					page: this.page,
					size: this.size,
					data_type: 2,
					style: 10,
				}

				if (type == 6) {
					this.role = 6
					Object.assign(params,{role_id: 6}) 
				}else{
					this.role = 4
					Object.assign(params,{role_id: 4}) 
				}

				this.$ajax({
					url: 'api/org/class_something',
					method: 'get',
					params: params,
					func: {success: success}
				})
			},
			/*学期改变时，重置数据*/
			changeQuality(val){
				if (this.semester == '') {
					this.studentList = []
					this.teacherList = []
					this.userNameList = []
				}else{
					this.semester = val

				}
			
			},
			//请求学期数据
			getSemester(){
				let success = (res=>{

					this.SemesterList = res.data;
					this.semester = res.data.length > 0 ? res.data[0].id : ''
					for (var i = 0; i < this.SemesterList.length; i++) {
						if (this.SemesterList[i].semester == 1) {
							this.SemesterList[i].name = this.SemesterList[i].start_year +'-'+ this.SemesterList[i].end_year + '学年 第一学期'
						}else{
							this.SemesterList[i].name = this.SemesterList[i].start_year +'-'+ this.SemesterList[i].end_year + '学年 第二学期'
						}
					}

					this.getEvaluate()
				});

				let params = {
					now_term: 1
				};

				this.$ajax({
					url: 'api/quality/get_term',
					method: 'get',
					params: params,
					func: {success: success}
				})
			},
			/*选择评委*/
			changUser(type){
				this.page = 1
				this.selectTestView = true
				if (type == 1) {
					this.getUserList(6)
					
				}else{
					this.getUserList(4)
				}
			},

			//选中的试卷--列表
			changeTestChecked(val){
				this.testChecked = val;
				// this.testChecked = val;
			},
			//搜索-试卷
			changeSearch(){
				this.page_index2 = 1
				this.getTestList()
			},
			changePage(val){
				this.page = val;
				this.getUserList(this.role)
			}
		}
	}
</script>